/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package game;

import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
/**
 *
 * @author jarrett
 */
public class Ball {
    double x;
    double y;
    double vx;
    double vy;
    double speed;
    int diameter;
    double restitution;
    Game game;
    
    public Ball(int diameter, float vel, float sp) {
        x = 50;
        y = 50;
        vx = vel;
        vy = vel;
        speed = sp;
        //Normalize the velocity vector
        double length = Math.sqrt((vx*vx)+(vy*vy));
        vx = vx/length;
        vy = vy/length;
        
        restitution = .95;
        this.game = game;
        this.diameter = diameter;
    }
    
    public void updatePosition() {
        x+=(vx*speed);
        y+=(vy*speed);
        vy += .2;
    }
    
    public void checkBounds(Game game) {
        int width = game.getWidth();
        int height = game.getHeight();
        
        boolean collision = false;
        
        if (x >= (width - diameter)) {
            vx = -vx;
            collision = true;
        }
        if (y >= (height - diameter)) {
            vy = -vy;
            collision = true;
        }
        if (x <= 0) {
            vx = -vx;
            collision = true;
        }
        if (y <= 0) {
            vy = -vy;
            collision = true;
        }
        if (collision) {
            vx *= restitution;
            vy *= restitution;
        }
    }
    
    public void checkCollision(Ball b) {
    }
    
    public void drawBall(Graphics2D g) {
        g.fillOval((int)x, (int)y, diameter, diameter);
    }
}
